package com.happy.system.controller;

import com.happy.common.core.utils.StringUtils;
import com.happy.common.core.utils.poi.ExcelUtil;
import com.happy.common.core.web.controller.BaseController;
import com.happy.common.core.web.domain.AjaxResult;
import com.happy.common.core.web.page.TableDataInfo;
import com.happy.common.log.annotation.Log;
import com.happy.common.log.enums.BusinessType;
import com.happy.common.security.annotation.RequiresPermissions;
import com.happy.common.security.utils.SecurityUtils;
import com.happy.system.api.domain.SysDept;
import com.happy.system.domain.Employee;
import com.happy.system.service.IEmployeeService;
import com.happy.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags ="员工管理接口")
public class EmployeeController extends BaseController {
    @Autowired
    IEmployeeService employeeService;

    @Autowired
    ISysDeptService deptService;

    /**获取员工列表**/
    @RequiresPermissions("system:employee:list")
    @GetMapping("/list")
    @ApiOperation(value = "获取员工列表",notes = "该接口实现获取员工列表功能")
    public TableDataInfo list(Employee employee)
    {
        startPage();
        List<Employee> list = employeeService.selectEmployeeList(employee);
        return getDataTable(list);
    }


    @Log(title = "员工管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:employee:import")
    @PostMapping("/importData")
    @ApiOperation(value = "文件导入员工数据",notes = "该接口实现文件导入员工数据功能")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        List<Employee> employeeList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = employeeService.importEmployee(employeeList, updateSupport, operName);
        return success(message);
    }

    /**新增员工**/
    @PostMapping
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:employee:add")
    @ApiOperation(value = "手动逐个新增员工", notes = "该接口实现手动逐个新增员工功能")
    public AjaxResult add(@Validated @RequestBody Employee employee) {
        if (StringUtils.isNotEmpty(employee.getPhone()) && !employeeService.checkPhoneUnique(employee)) {
            return error("新增员工'" + employee.getName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(employee.getEmail()) && !employeeService.checkEmailUnique(employee)) {
            return error("新增员工'" + employee.getName() + "'失败，邮箱账号已存在");
        }
        return toAjax(employeeService.insertEmployee(employee));
    }


    @PostMapping("/importTemplate")
    @ApiOperation(value = "导入下载模板下载",notes = "该接口实现导入模板下载功能")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        util.importTemplateExcel(response, "员工数据");
    }

    /**获取部门树列表**/
    @RequiresPermissions("system:user:list")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }

    /**根据员工编号删除员工**/
    @RequiresPermissions("system:employee:remove")
    @DeleteMapping("/{employeeIds}")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @ApiOperation(value="删除指定员工员工",notes = "该接口实现删除指定员工功能")
    public AjaxResult remove(@PathVariable Long employeeIds[]){
        return toAjax(employeeService.deleteEmployeeByIds(employeeIds));
        //TODO 删除云存储上的图片
    }

    /**导出员工**/
    @RequiresPermissions("system:employee:export")
    @PostMapping("/export")
    @Log(title = "员工管理", businessType = BusinessType.EXPORT)
    @ApiOperation(value="导出员工信息到excel表格",notes = "该接口实现导出员工信息到excel表格功能")
    public void export(HttpServletResponse response, Employee employee)
    {
        List<Employee> list = employeeService.selectEmployeeList(employee);
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        util.exportExcel(response, list, "员工数据");
    }

    /**修改员工**/
    @RequiresPermissions("system:employee:edit")
    @PutMapping
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value="修改员工信息",notes = "该接口实现修改员工信息功能")
    public AjaxResult edit(@Validated @RequestBody Employee employee) throws IOException {
        if(employee.getEmployeeId() == null) {
            return error("修改员工：失败，员工id为空");
        }
        employeeService.EditEmployee(employee);
        return success();
    }

    /**返回员工详情**/
    @RequiresPermissions("system:employee:query")
    @GetMapping(value = { "/", "/{employeeId}" })
    @ApiOperation(value="根据员工Id获取员工详情",notes = "该接口实现根据员工Id获取员工详情功能")
    public AjaxResult getInfo(@PathVariable(value = "employeeId", required = false) Long employeeId) throws IOException {
        return success(employeeService.queryEmployeeById(employeeId));
    }
}
